package com.iss.okair.db.mapper.sql;

import com.iss.common.StringTools;
import com.iss.okair.db.entity.AssetsPermanent;
import com.iss.okair.db.model.AssetsPermanentModel;
import org.apache.ibatis.jdbc.SQL;

/*
* 存放与assets_permanent表有关系的生成动态sql语句方法的类
* */

public class AssetsPermanentSQL {
    //生成条件查询的动态sql语句的方法
    public String find(AssetsPermanentModel model) {
        return new SQL() {{
            SELECT("*");
            FROM("assets_permanent");
            if (model.getAssetsNameCn() != null && !model.getAssetsNameCn().isEmpty()) {
                WHERE("assetsNameCn like '%" + model.getAssetsNameCn() + "%'");
            }
            if (model.getAssetsCode() != null && !model.getAssetsCode().isEmpty()) {
                WHERE("assetsCode like '%" + model.getAssetsCode() + "%'");
            }
            if (model.getAddress() != null && !model.getAddress().isEmpty()) {
                WHERE("address like '%" + model.getAddress() + "%'");
            }
            if (model.getUtil() != null && !model.getUtil().isEmpty()) {
                WHERE("util like '%" + model.getUtil() + "%'");
            }
            if (model.getCity() != null && !"-1".equals(model.getCity())) {
                WHERE("city=" + model.getCity());
            }
            if (model.getEquityType() != null && !"-1".equals(model.getEquityType())) {
                WHERE("equityType=" + model.getEquityType());
            }
            if (model.getAssetsType2() != null && !"-1".equals(model.getAssetsType2())) {
                WHERE("assetsType=" + model.getAssetsType2());
            }
            if (model.getAssetsType1() != null && !"-1".equals(model.getAssetsType1()) && "-1".equals(model.getAssetsType2())) {
                WHERE("assetsType in (select id from category where parentId=" + model.getAssetsType1() + ")");
            }
        }}.toString();
    }

    //生成修改的动态sql语句的方法
    public String update(AssetsPermanent permanent) {
        return new SQL() {{
            UPDATE("assets_permanent");
            if (permanent.getAssetstype() != null && permanent.getAssetstype() != -1) {
                SET("assetstype=#{assetstype}");
            }
            if (!StringTools.isNullOrEmpty(permanent.getNamecn())) {
                SET("namecn=#{namecn}");
            }
            if (permanent.getEquitytype() != null && permanent.getEquitytype() != -1) {
                SET("equitytype=#{equitytype}");
            }
            if (!StringTools.isNullOrEmpty(permanent.getAddress())) {
                SET("address=#{address}");
            }
            if (permanent.getArea() != null) {
                SET("area=#{area}");
            }
            if (!StringTools.isNullOrEmpty(permanent.getCity())) {
                SET("city=#{city}");
            }
            if (permanent.getScale() != null && permanent.getScale() != -1) {
                SET("scale=#{scale}");
            }
            if (!StringTools.isNullOrEmpty(permanent.getUtil())) {
                SET("util=#{util}");
            }
            if (!StringTools.isNullOrEmpty(permanent.getComment())) {
                SET("comment=#{comment}");
            }
            WHERE("id=#{id}");
        }}.toString();
    }
}